<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Controller;
use \think\Log;
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class HomeController extends CardController {
    
    //protected $login_session_id = 'DFDSFDFslfsfdlsdidsf';
    protected $data = array();


    //素材库首页
    public function index($category = 3,$p = 1){        
        $page = intval($p);
        $category = intval($category);
        $category = $category ? $category : 3;
        $page = $page ? $page : 1; //默认显示第一页数据
        $login_show = 'off';
        if ($page > 1){
            if (!$this->checkLogin()){
                $page = 1;
                $login_show = 'on';
                //$this->mustLogin();
            }
        }

        //获取素材标签
        $category_list = M('category_items')->where(['status'=>1])->order(['pc_displayorder'=>'desc','id'])->select();
        foreach ($category_list as $cur_category){
            $cur_category['id'] == $category && $comefrom = $cur_category['title'];
        }
        $first_category_list = array_slice($category_list, 0,7);
        $second_category_list = array_slice($category_list, 7);
        //获取字段
        $fields= ['id','img','title','createtime','city','brand','class','s_url','t_url'];
        $map['status'] = 1;
        $map['pc_status'] = 1;
                if ($category == 3){
            $map['category'] = array('in',array(1,3));
        }else{
            $map['category'] = $category;   
        }
        $map['s_url'] = array('NEQ','');
        $row = 30;
        $data = M('listitems')
                /* 查询指定字段，不指定则查询所有字段 */
                ->field($fields)
                // 查询条件
                ->where($map)
                /* 默认通过id逆序排列 */
                ->order(['pc_displayorder'=>'desc','id'])
                /* 数据分页 */
                ->page($page, $row)
                /* 执行查询 */
                ->select();

            /* 查询记录总数 */
            $count = M('listitems')->where($map)->count();

        //分页
        if($count > $row){
            $page = new \Think\Page($count, $row);
            $page->rollPage=6;
            $page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $page->url = 'home.html?';
            $this->assign('_page', $page->show());
        }

        
        
        $this->data = $data;
        $this->assign('_count', $count);
        $this->assign('comefrom', $comefrom);
        $this->assign('data',$data);
        $this->assign('first_category_list',$first_category_list);
        $this->assign('second_category_list',$second_category_list);
        $this->assign('_member', $this->_member);
        $this->assign('title', $cur_category['title'] .'图库-资讯-梧桐珆');
        $this->assign('login_show', $login_show);
        $this->display();
    }
    
    //后置方法
//     public function _after_index(){
        
//         foreach ($this->data as $my_pirture){
            
//             if (empty($my_pirture['s_url'])){
//                 $tietu_result = tietu($my_pirture['img']);
//                 $re_result = $this->recordTietu('listitems',$tietu_result,$my_pirture['id']);
// //                if (!$re_result){
// //                    break;
// //                }
//             }
//         }
//     }

    //图集子集
    public function gallery(){  
    //var_dump($_COOKIE,$_SESSION);exit;
        $id = I('get.id',0,'int');
        $p = I('get.p',1,'int');
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $login_show = 'off';
        if ($page > 1){
            if (!$this->checkLogin()){
                $page = 1;
                $login_show = 'on';
            }
        }
        //获取城市信息
        $listitems = M('listitems')->where(['id'=>$id])->field(['city','title','category','brand','class'])->limit(1)->select();
        empty($listitems) &&  $this->error('无分类数据');
        $category_items = M('category_items')->where(['id'=>$listitems[0]['category']])->field(['title','id'])->limit(1)->select();
        //获取字段
        $fields= ['id','img','title','createtime','t_url','image','s_url','t_url'];
        $map['status'] = 1;
        $map['pc_status'] = 1;
        $map['category'] = $category;
        $map['cat_code'] = $id;
        $map['s_url'] = array('NEQ','');
        $row = 30;//30
        $data = M('items')
                /* 查询指定字段，不指定则查询所有字段 */
                ->field($fields)
                // 查询条件
                ->where($map)
                /* 默认通过id逆序排列 */
                ->order(['pc_displayorder'=>'desc','id'])
                /* 数据分页 */
                ->page($page, $row)
                /* 执行查询 */
                ->select();

            /* 查询记录总数 */
            $count = M('items')->where($map)->count();
        //分页
        if($count > $row){
            $page = new \Think\Page($count, $row);
            $page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $page->url = 'gallery.html?';
            $this->assign('_page', $page->show());
        }
   
        $this->data = $data;
        $this->assign('_count', $count);

        $this->assign('data', $data);
        $this->assign('listitems', $listitems);
        $this->assign('category_items', $category_items);
        $this->assign('_member', $this->_member);
        $this->assign('title',  $listitems[0]['title'].$category_items[0]['title']." 图库-资讯-梧桐珆");
        $this->assign('login_show', $login_show);
        $this->display('gallery');
    }

    //后置方法
    // public function _after_gallery(){
        
    //     foreach ($this->data as $my_pirture){

    //         if (empty($my_pirture['s_url'])){
    //             $tietu_result = tietu($my_pirture['image']);
    //             $r = $this->recordTietu('items',$tietu_result,$my_pirture['id']);
    //             if ($r === FALSE){
    //                 break;
    //             }
    //         }
    //     }
    // }
    
    
    //图片详情
    public function gallery_detail(){
        
        $detail_id = I('get.id',0,'int');
        $fields = ['id','img','image','s_url','t_url','title','createtime'];
        $data = M('items')->where(['id'=>$detail_id,'status'=>1,'pc_status'=>1])->getBYfield($fields);

        //获取导航
        $items_id = M('items')->where(['id'=>$detail_id])->getfield('cat_code');
        $listitems = M('listitems')->where(['id'=>$items_id])->field(['id,city','title','category'])->limit(1)->select();
        empty($listitems) &&  $this->error('无分类数据');
        $category_items = M('category_items')->where(['id'=>$listitems[0]['category']])->field(['title','id'])->limit(1)->select();
        $this->assign('data',$data);
        $this->assign('listitems', $listitems);
        $this->assign('category_items', $category_items);
        $this->assign('title', $listitems[0]['title'].$category_items[0]['title']." 图库-资讯-梧桐珆");
        $this->assign('_member', $this->_member);
        $this->display('gallery_detail');
    }
    
    
    /*记录贴图库数据*/
    private function recordTietu($table = 'items',$data = array(),$id=null){

        if (empty($table)){
            $this->error('缺少数据表');
        }
        if (empty($data)){
            $this->error('缺少返回数据');
        }
        if (empty($id)){
            $this->error('缺少id');
        }
                   Log::record('贴图库错误:'.$data['code'],'NOTIC'); 
        //记录错误
        if ($data['code']){
           Log::record('贴图库错误:'.$data['info'],'NOTIC'); 
           return FALSE;
        }else{
            $itmesModel = M($table);
            $itmesModel->s_url = $data['s_url'];
            $itmesModel->t_url = $data['t_url'];
            $itmesModel->width = $data['width'];
            $itmesModel->height = $data['height'];
            $result = $itmesModel->where('id='.$id)->save();
            if ( false === $result ){
                Log::record('贴图库错误:'.$id.'更新失败','NOTIC');
                return FALSE;
            }
        }
    }
}	